<div class="row">
	<div class="col-xs-12">
		<div class="box">
			<div class="box-header">
				<h3 class="box-title">公开资源</h3>

				<div class="box-tools">
					<div class="input-group input-group-sm" style="width: 350px;">
						<!-- <input type="text" name="table_search" id="searchNameId"
							class="form-control pull-right" placeholder="用户名"> -->

						<div class="input-group-btn">
							<!-- <button type="button" class="btn btn-default btn-search">
                                <i class="fa fa-search"></i>
							</button> -->
						</div>
					</div>
				</div>
			</div>
			<!-- /.box-header -->
			<div class="box-body table-responsive no-padding">
				<table class="table table-hover">
					<thead>
						<tr>
							<td>当前位置:</td>
							<td><input id="newFolder" type="button" value="新建文件夹"/></td>
						</tr>
						<tr>
							<td id="path"></td>
						</tr>
						<tr>
							<th>文件名</th>
							<!-- <th>作者</th>
							<th>最后更新时间</th> -->
						</tr>
					</thead>
					<tbody id="tbodyId">
					    <tr>
					       <td>数据正在加载中...</td>
					    </tr>
					</tbody>
					<tfoot id="tfootId">
						<tr>
							<td>数据正在加载中...</td>
						</tr>
					</tfoot>
				</table>
			</div>
			</div>
			<!-- /.box-body -->
		</div>
		<!-- /.box -->
	</div>

<script src="bower_components/jquery/dist/jquery.form.min.js"></script>
<script type="text/javascript">
	
	$(function() {
		$(document).ready(showList(""))
		$(document).ready(showUpload())
		$("#newFolder").on("click",newFolder)
		
		$("#upload").ajaxForm(function(result) {
			debugger
			if (result.state == 1) {
				alert("上传成功");
			} else {
				alert("上传失败,请重新上传");
			}
			var now = document.getElementById("path").innerText;
			showList(now);
			showUpload();
		});
	});
	
	function newFolder(){
		var folderName = prompt("请输入新建文件夹的名称");
		if(folderName){
			console.log(folderName);
			var now = document.getElementById("path").innerText;
			var folderPath = now + folderName;
			console.log("folderName="+now)
			var url = "common/newFolder";
			var param = {
				"path" : folderPath
			};
			$.post(url, param, function(result){
				alert(result.message)
			});
			showList(now);
			showUpload();
        }

	}

	function showList(path) {
		var url = "common/view";
		var param = {
			"path" : path
		};
		$("#path").html(path);
		console.log(param);
		$.post(url, param, function(result) {
			var tBody = $("#tbodyId");
			var tr = $("<tr></tr>");
			var tds = "<td><a href='#' onclick='OutsideFolder()'>.../</a></td>"
			tBody.empty();
			tr.append(tds);
			tBody.append(tr);
			var arr = result.data
			var now = document.getElementById("path").innerText;
			$.each(arr, function(n, arr) {
				//2.1创建tr
				var tr = $("<tr></tr>");
				//2.2创建多个td
				if (arr.isFile) {
					var tds = doCreateFileTds(arr, now);
				} else {
					var tds = doCreateFolderTds(arr);
				}
				//2.3将td追加到tr中
				tr.append(tds);
				//2.4将tr追加到tbody中
				tBody.append(tr);
			});
		});
	}

	function showUpload() {
		var now = document.getElementById("path").innerText;
		var tfootId = $("#tfootId");
		var tr = $("<tr></tr>");
		var td = "<td><form id='upload' action='common/upload?filePath="+ now + "' method='post' enctype='multipart/form-data'>上传文件到当前文件夹：<input type='file' name='file' /> <input type='submit' /></form></td>"
		console.log(td);
		tfootId.empty();
		tr.append(td);
		tfootId.append(tr);
	}

	function doCreateFileTds(arr, now) {
		var tds = "<td><a href='common/download/" + arr.fileName + "?filePath="
				+ now + "'>" + arr.fileName + "</a></td>"
		/* + "<td>" + arr.updateTime + "</td>" 
		+ "<td>" + arr.uploader+ "</td>"; */
		return tds;
	}
	function doCreateFolderTds(arr) {
		var tds = "<td><a href='#' onclick= 'intoFolder(&apos;" + arr.fileName
				+ "/&apos;)'>" + arr.fileName + "/</a></td>"
		return tds;
	}
	function intoFolder(folder) {
		var now = document.getElementById("path").innerText;
		path = now + folder;
		showList(path);
		showUpload();
		return false;
	}
	function OutsideFolder() {
		var now = document.getElementById("path").innerText;
		var path = "";
		arr = now.split("/");
		for (var i = 0; i < arr.length - 2; i++) {
			path = path + arr[i] + "/"
		}
		showList(path);
		showUpload();
		return false;
	}
</script>



